package cn.itcast.service.targo.impl;


import cn.itcast.dao.cargo.ContractDao;
import cn.itcast.dao.cargo.ContractProductDao;
import cn.itcast.dao.cargo.ExtCproductDao;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ContractProductService;
import cn.itcast.vo.ContractProductVo;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

@Service(timeout = 100000)
public class ContractProductServiceImpl implements ContractProductService {
    @Autowired
    private ContractProductDao contractProductDao;

    @Autowired
    private ContractDao contractDao;

    @Autowired
    private ExtCproductDao extCproductDao;
    /*
    分页查询
     */
    @Override
    public PageInfo<ContractProduct> findByPage(ContractProductExample example, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<ContractProduct> list = contractProductDao.selectByExample(example);
        return new PageInfo<>(list);
    }

    /*
    查询所有货物
     */
    @Override
    public List<ContractProduct> findAll(ContractProductExample contractProductExample) {
        return contractProductDao.selectByExample(contractProductExample);
    }

    @Override
    public ContractProduct findById(String id) {
        return contractProductDao.selectByPrimaryKey(id);
    }

    @Override
    public void save(ContractProduct contractProduct) {
        //1.设置主键
        contractProduct.setId(UUID.randomUUID().toString());
        //2.计算货物金额
        Double price = contractProduct.getPrice();
        Integer cnumber = contractProduct.getCnumber();
        Double amount = 0d;
        if(price != null && cnumber!=null ){
            amount = price * cnumber;
        }
        //3.设置货物金额
        contractProduct.setAmount(amount);


        //4.1修改购销合同
        Contract contract = contractDao.selectByPrimaryKey(contractProduct.getContractId());
        //4.2设置购销合同总金额 = 总金额 + 货物金额
        contract.setTotalAmount(contract.getTotalAmount() + amount);
        //4.3设置购销合同货物数量
        contract.setProNum(contract.getProNum() + 1);

        //4.4执行修改购销合同（动态修改）
        contractDao.updateByPrimaryKeySelective(contract);

        //5.货物添加
        contractProductDao.insertSelective(contractProduct);
    }

    @Override
    public void update(ContractProduct contractProduct) {
        //1. 修改后的货物金额
        Double amount = 0d;
        if (contractProduct.getCnumber() != null && contractProduct.getPrice() != null) {
            amount = contractProduct.getCnumber() * contractProduct.getPrice();
        }
        contractProduct.setAmount(amount);


        //2.根据货物id查询修改前的金额
        Double oldAmount = contractProductDao.selectByPrimaryKey(contractProduct.getId()).getAmount();
        //根据购销合同id查询
        Contract contract = contractDao.selectByPrimaryKey(contractProduct.getContractId());
        //设置购销合同总金额
        contract.setTotalAmount(contract.getTotalAmount() + amount - oldAmount);


        //执行更新购销合同
        contractDao.updateByPrimaryKeySelective(contract);

        //执行更新货物信息
        contractProductDao.updateByPrimaryKeySelective(contractProduct);
    }

    // 货物删除
    @Override
    public void delete(String id) {
        //1.获取货物金额
        ContractProduct contractProduct = contractProductDao.selectByPrimaryKey(id);
        Double cpAmount = contractProduct.getAmount();

        //2.根据货物id查询货物下所有附件
        ExtCproductExample example = new ExtCproductExample();
        example.createCriteria().andContractProductIdEqualTo(id);

        List<ExtCproduct> extCpList = extCproductDao.selectByExample(example);

        Double extAmount =0d;
        if (extCpList != null && extCpList.size() > 0) {
            for (ExtCproduct extCproduct : extCpList) {
                extAmount += extCproduct.getAmount();
                extCproductDao.deleteByPrimaryKey(extCproduct.getId());
            }
        }

        //3.修改购销合同
        //通过货物的购销合同id获取货物购销合同对象
        Contract contract = contractDao.selectByPrimaryKey(contractProduct.getContractId());
        //设置购销合同总金额= 总金额-附件金额-货物金额
        contract.setTotalAmount(contract.getTotalAmount() - extAmount -cpAmount);
        //设置购销合同货物数量
        contract.setProNum(contract.getProNum() -1);
        //设置购销合同附件数量
        contract.setExtNum(contract.getExtNum() - extCpList.size());
        //执行修改
        contractDao.updateByPrimaryKeySelective(contract);

        //删除货物
        contractProductDao.deleteByPrimaryKey(id);
    }

    @Override
    public List<ContractProductVo> findByShipTime(String inputDate) {
        return contractProductDao.findByShipTime(inputDate);
    }
}
